package com.ohc.其它.网络编程;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServerDemo01 {
    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket=null;
        Socket socket=null;
        InputStream is=null;
        ByteArrayOutputStream baos=null;
        //1.创建服务
        try {
            serverSocket = new ServerSocket(9999);
            //2.监听客户端的连接
            socket = serverSocket.accept();//阻塞式监听，会一直等待客户端连接
            //3.获取输入流
            is = socket.getInputStream();
            int read = is.read();
//            is.
            //管道流
            baos=new ByteArrayOutputStream();
            byte[] buffer=new byte[1024];
            int len;
            while ((len=is.read(buffer))!=-1){
                baos.write(buffer,0,len);
            }
            System.out.println(baos.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            baos.close();
            is.close();
            socket.close();
            serverSocket.close();
        }
    }

}
